package com.csx.base.design.behavior.chainOfResponsibility.example.exampleA.handler;

import com.csx.base.design.behavior.chainOfResponsibility.example.exampleA.model.vo.ProductVO;
import com.csx.base.design.behavior.chainOfResponsibility.example.exampleA.model.vo.Result;
import com.csx.base.design.behavior.chainOfResponsibility.example.exampleA.model.vo.ErrorMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author cuisongxu
 * @date 2022/12/28 22:36
 */
@Component
public class NullValueCheckHandler extends AbstractCheckHandler {

    private final Logger logger = LoggerFactory.getLogger(NullValueCheckHandler.class);

    @Override
    public Result handle(ProductVO param) {
        logger.info(this.getClass().getName() + "handle check begin");

        // 降级: 如果配置了降级，则跳过该处理器，执行下一个处理器
        if(super.getConfig().getDown()) {
            logger.info(this.getClass().getName() + "have been down, skip this handler");
            return super.next(param);
        }

        // 参数必填校验
        if(Objects.isNull(param)) {
            return Result.failure(ErrorMsg.PARAM_NULL_ERROR);
        }

        // SkuId 商品主键参数必填校验
        if(Objects.isNull(param.getSkuId())) {
            return Result.failure(ErrorMsg.PARAM_SKU_NULL_ERROR);
        }

        // Price 价格参数必填校验
        if(Objects.isNull(param.getPrice())) {
            return Result.failure(ErrorMsg.PARAM_PRICE_NULL_ERROR);
        }

        if(Objects.isNull(param.getStock())) {
            return Result.failure(ErrorMsg.PARAM_STOCK_NULL_ERROR);
        }

        logger.info(this.getClass().getName() + "handle check pass");

        // 执行下一个处理器
        return super.next(param);
    }
}
